package com.example.tb_base.base_activity_fragment

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.tb_base.AnalysisPage

/**
 * Activity基类，统一处理布局加载、WindowInsets适配、页面统计等通用逻辑。
 * 子类只需实现抽象方法即可快速搭建自己的Activity。
 */
abstract class BaseActivity : AppCompatActivity(), AnalysisPage {

    override fun onCreate(savedInstanceState: Bundle?) {
        uninitializedContentView()
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        initializeContentView()
        rootView()?.let {
            ViewCompat.setOnApplyWindowInsetsListener(it) { v, insets ->
                val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
                v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
                insets
            }
        }
        onActivityCreated(savedInstanceState)
    }

    protected abstract fun layoutId(): Int
    protected abstract fun rootView(): View?
    protected abstract fun onActivityCreated(savedInstanceState: Bundle?)
    final override fun getPageName(): String = javaClass.simpleName
    protected open fun uninitializedContentView() {}
    protected open fun initializeContentView() {
        setContentView(layoutId())
    }
}